package sg.edu.nus.dbd.dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.dbd.dao.ConfigurationDAO;
import sg.edu.nus.dbd.formbean.ThresholdConfig;
import sg.edu.nus.dbd.util.Constants;

public class ConfigurationDAOImpl extends BaseDAO implements ConfigurationDAO {
	public List<ThresholdConfig> getThresholdParameters() {
		Connection conn = getConnection();
		List<ThresholdConfig> configList = new ArrayList<ThresholdConfig>();
		PreparedStatement ps = null;
		ResultSet rs = null;

		if (conn != null) {
			try {
				ps = conn.prepareStatement("select * from threshold_param");
				rs = ps.executeQuery();
				while (rs.next()) {
					ThresholdConfig config = new ThresholdConfig();
					config.setId(rs.getInt("id"));
					config.setName(rs.getString("param_name"));
					config.setMinLevel(rs.getDouble("param_min"));
					config.setMaxLevel(rs.getDouble("param_max"));
					config.setWarningLevel(rs.getDouble("param_warning"));
					config.setCriticalLevel(rs.getDouble("param_critical"));
					config.setMetricName(((String)Constants.DASHBOARD_PARAMS_METRIC.get(rs.getString("param_name"))));
                    config.setInverse(((Boolean)Constants.DASHBOARD_PARAMS_INVERSE.get(rs.getString("param_name"))).booleanValue());
					configList.add(config);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				closeStatement(ps);
				closeResultSet(rs);
				closeConnection(conn);
			}

		}
		return configList;
	}

	public ThresholdConfig getThresholdParameter(int thParamId) {
		Connection conn = getConnection();
		ThresholdConfig config = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		if (conn != null) {
			try {
				ps = conn.prepareStatement("select * from threshold_param where id=?");
				ps.setInt(1, thParamId);
				rs = ps.executeQuery();
				if (rs.next()) {
					config = new ThresholdConfig();
					config.setId(rs.getInt("id"));
					config.setName(rs.getString("param_name"));
					config.setMinLevel(rs.getDouble("param_min"));
					config.setMaxLevel(rs.getDouble("param_max"));
					config.setWarningLevel(rs.getDouble("param_warning"));
					config.setCriticalLevel(rs.getDouble("param_critical"));
					config.setMetricName(((String)Constants.DASHBOARD_PARAMS_METRIC.get(rs.getString("param_name"))));
                    config.setInverse(((Boolean)Constants.DASHBOARD_PARAMS_INVERSE.get(rs.getString("param_name"))).booleanValue());
				}
			} catch (SQLException e) {				
				e.printStackTrace();
			} finally {
				closeStatement(ps);
				closeResultSet(rs);
				closeConnection(conn);
			}

		}
		return config;
	}

	public ThresholdConfig getThresholdParameter(String paramName) {
		Connection conn = getConnection();
		ThresholdConfig config = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		if (conn != null) {
			try {
				ps = conn.prepareStatement("select * from threshold_param where param_name=?");
				ps.setString(1, paramName);
				rs = ps.executeQuery();
				if (rs.next()) {
					config = new ThresholdConfig();
					config.setId(rs.getInt("id"));
					config.setName(rs.getString("param_name"));
					config.setMinLevel(rs.getDouble("param_min"));
					config.setMaxLevel(rs.getDouble("param_max"));
					config.setWarningLevel(rs.getDouble("param_warning"));
					config.setCriticalLevel(rs.getDouble("param_critical"));
					config.setMetricName(((String)Constants.DASHBOARD_PARAMS_METRIC.get(rs.getString("param_name"))));
                    config.setInverse(((Boolean)Constants.DASHBOARD_PARAMS_INVERSE.get(rs.getString("param_name"))).booleanValue());
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				closeStatement(ps);
				closeResultSet(rs);
				closeConnection(conn);
			}

		}
		return config;
	}

	public boolean updateThresholdConfig(ThresholdConfig config) {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		if (conn != null) {
			try {
				ps = conn.prepareStatement("update threshold_param set param_min=?, param_max=?, param_critical=?, param_warning=? where id=?");
				ps.setDouble(1, config.getMinLevel());
				ps.setDouble(2, config.getMaxLevel());
				ps.setDouble(3, config.getCriticalLevel());
				ps.setDouble(4, config.getWarningLevel());
				ps.setInt(5, config.getId());
				count = ps.executeUpdate();

			} catch (SQLException e) {		
				e.printStackTrace();
			} finally {
				closeStatement(ps);
				closeResultSet(rs);
				closeConnection(conn);
			}

		}
		return (count > 0);
	}
}